package com.edu.edumanager.service.imp;

import com.edu.edumanager.bean.EduChapter;
import com.edu.edumanager.bean.EduVideo;
import com.edu.edumanager.exception.EduException;
import com.edu.edumanager.mapper.EduChapterMapper;
import com.edu.edumanager.service.ChapterService;
import com.edu.edumanager.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-21 23:21
 **/
@Service
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    EduChapterMapper chapterMapper;
    @Autowired
    VideoService videoService;
    /**
     *添加章节代码
     * @param chapter
     * @return 成功条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addChapter(EduChapter chapter) {
        Date date = new Date();
        chapter.setGmtCreated(date);
        chapter.setGmtModified(date);
        int row = chapterMapper.addChapter(chapter);
        if (row==0){
            throw new EduException("章节添加失败");
        }

        List<EduVideo> videoList = chapter.getVideoList();

        for (EduVideo v:videoList) {
            v.setCourseId(chapter.getCourseId());
            v.setChapterId(chapter.getId());
            int j = videoService.addVideo(v);
            if(j==0){
                throw new EduException("添加小节失败");
            }
        }
        return row;
    }

    /**
     * 依照章节ID查询该章节信息
     * @param chapterId
     * @return 章节集合
     */
    @Override
    public EduChapter getChapterById(Integer chapterId) {
        EduChapter chapterList = chapterMapper.getChapterById(chapterId);
        return chapterList;
    }
    @Override
    public List<EduChapter> getChapterList(Integer courseId) {
        // 查询1级列表
        List<EduChapter> chaptersOne = chapterMapper.findChapterAndVideo(courseId);
        return chaptersOne;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteChapterById(Integer chapterId) {
        //我们这里只删除了数据库 需要删除图片服务器上面的视频或者图片吗 ？ 要删除也可以 自己用io来做
        //但是呢 一般来说 我们的视频 放在阿里云上面的 我们都没有删除的
        int i = videoService.deleteVideoByChapterId(chapterId);

        int j = chapterMapper.deleteChapterId(chapterId);
        if(j==0){
            throw new EduException("删除章节失败");
        }

        return j;
    }

    @Override
    public int updateChapter(EduChapter chapter) {
        Date date = new Date();
        chapter.setGmtModified(date);
        int i = chapterMapper.updateChapter(chapter);
        if (i == 0) {
            throw new EduException("修改章节失败");
        }
        List<EduVideo> videoList = chapter.getVideoList();
        if (videoList.size() == 0) {
            return i;
        } else {
            //1.吧没有小节id的添加完成 应该去调用添加小节service
            for (EduVideo video:videoList) {
                if(video.getId()==null){
                    video.setChapterId(chapter.getId());
                    video.setCourseId(chapter.getCourseId());
                    videoService.addVideo(video);
                }
            }
        }
        return i;
    }

    @Override
    public int deleteChapterByCourseId(Integer courseId) {
        int i = chapterMapper.deleteChapterByCourseId(courseId);
        return i;
    }

}
